﻿Imports System.Data.OleDb

Public Class CarInDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        'This handle comment gridview when the user come from a differnt page.
        If IsPostBack = False Then

            'If there is any error with car ID it will take them back to CarList page.
            If Request.Params("ID") = "" Then Response.Redirect("CarList.aspx")

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Car_Comment WHERE CarIDFK=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))

            oleDbConn.Open()

            'Get the data back and fill the gridview. 
            Dim dataReaders = cmd.ExecuteReader()
            GridViewCarComment.DataSource = dataReaders
            GridViewCarComment.DataBind()

            'Here we are going to keep user activity log for viewing the CarInDetail page

            Dim CarViewedSql As String = "INSERT INTO CarViewed (Viewer, CarIDFK) VALUES (@f1,@f2)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(CarViewedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text

            cmd2.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd2.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmd2.ExecuteNonQuery()






        End If
    End Sub

    Protected Sub btnBackup_Click(sender As Object, e As EventArgs) Handles btnBackup.Click
        Response.Redirect("CarList.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btnCarComment_Click(sender As Object, e As EventArgs) Handles btnCarComment.Click


        If tbxCarComment.Text = "" Then
            MsgBox("ERROR please enter a comment!.")

        Else

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
            Dim SqlString As String = "Insert into Car_Comment(CommentBody,DateUploaded,TimeUploaded, UserIDFK, CarIDFK) Values (@f1,@f2,@f3,@f4,@f5)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text

            cmd.Parameters.AddWithValue("@f1", tbxCarComment.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd.Parameters.AddWithValue("@f5", Request.Params("ID"))

            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            'Response.Redirect("CarInDetail.aspx?ID=" & Request.QueryString("ID"))
            ' Fill grid view after the user fills comments in

            Dim fillComments As String = "SELECT * FROM Car_Comment WHERE CarIDFK=@f1"
            Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
            fillCommentsCmd.CommandType = CommandType.Text
            fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))

            Dim dataReaders = fillCommentsCmd.ExecuteReader()
            GridViewCarComment.DataSource = dataReaders
            GridViewCarComment.DataBind()
            'Clear textboxes detail
            tbxCarComment.Text = ""
        End If


    End Sub

    Protected Sub GridViewCarInDetail_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub

    Protected Sub btnTagCar_Click(sender As Object, e As EventArgs) Handles btnTagCar.Click
        'database connection and SQL string
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into Cars_Tags(CarIDFK, TagIDFK) Values (@f1,@f2)"
        oleDbConn.Open()


        For Each cb As ListItem In CBLCarTags.Items
            'here we are adding the checkox vaue with the database value
            If cb.Selected Then
                'here is the linkwe add the joins to the table of tags and cars

                
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If
        Next
        ' below here we are adding new Tags with a textbox
        If tbxAddNewTag.Text = "" Then Return

        'the textbox tags can be split with the line of code below.
        Dim newTags = tbxAddNewTag.Text.Split(",")
        'first add the new tag to the tag table. If you got a new tag it added it to the tag table
        For Each newTag In newTags
            Dim newTagSql As String = "Insert into Tags(TagName) Values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'then find the id of the new tag. When you have added the tag to the table you find the ID of the tag. 
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 TagID FROM Tags ORDER BY TagID DESC", oleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last ID with Car ID

            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newcmd.Parameters.AddWithValue("@f2", lastID)
            newcmd.ExecuteNonQuery()


        Next
    End Sub

    Protected Sub btn_Rating_Click(sender As Object, e As EventArgs) Handles btn_Rating.Click

        ' adding the rating of a car into the the rating table
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)

        Dim sql = "INSERT INTO CarRating (Rating, UsernameFKID, CarIDFK) VALUES (@f1,@f2,@f3)"
        Dim addCmd As New OleDbCommand(sql, conn)
        conn.Open()

        addCmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        addCmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        addCmd.Parameters.AddWithValue("@f3", Request.Params("ID"))

        addCmd.ExecuteNonQuery()
        conn.Close()



    End Sub
End Class